package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.CategoryData;
import data.UserData;
import debug.CategoryDebug;
import forms.CategoryForm;

import models.CategoryModel;
import models.UserModel;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

public class AddCategoryServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		
		TreeMap check = new TreeMap();
		CategoryData cData = new CategoryData();
		ArrayList<CategoryModel> categories = cData.list();
		CategoryModel category = new CategoryModel();
		
		request.setAttribute("check", check);
		request.setAttribute("categories", categories);
		request.setAttribute("category", category);
		request.setAttribute("hasChildren", false);
		request.setAttribute("form", "add");
		try {
			request.getRequestDispatcher("/pages/form_category.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		
		CategoryData cData = new CategoryData();
		CategoryModel category = new CategoryModel();
		
		UserModel user = (UserModel) request.getAttribute("user");
		
		//Check valid and get value form
		CategoryForm cForm = new CategoryForm();
		TreeMap forms = cForm.checkValid(request, category);
		category = (CategoryModel) forms.get("category");
		TreeMap check = (TreeMap) forms.get("checkValid");
		
		String remoteAddr = request.getRemoteAddr();
		ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
		reCaptcha.setPrivateKey("6LeA0c4SAAAAAIadFi6R4xiBkjLDo4jImT2S35LZ");
		
		String challenge = request.getParameter("recaptcha_challenge_field");
		String uresponse = request.getParameter("recaptcha_response_field");
		if(challenge == null)
		{
		        challenge = "b";
		}
		if(uresponse == null)
		{
		        uresponse = "a";
		}
		
		ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
		
		if (!reCaptchaResponse.isValid()) {
		        check.put("captcha", "Invalid Captchar");
		}
		
		if (check.size() == 0)
        {
			if (user != null) {
				category.setCreator(user.getUsername());
			}
			cData.insert(category);
			request.setAttribute("success", "Add success category " + category.getId());
			category = new CategoryModel();
        }
		
		ArrayList<CategoryModel> categories = cData.list();
		
		request.setAttribute("check", check);
		request.setAttribute("categories", categories);
		request.setAttribute("category", category);
		request.setAttribute("hasChildren", false);
		request.setAttribute("form", "add");
		try {
			request.getRequestDispatcher("/pages/form_category.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
